package com.qf.mpswaggershiro.weixin;

import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WxPayUtils {



        public static void main(String[] args) throws Exception {
            testPayURL();
//        final String s = WxpayUtils.queryOrder("20230127111905");
//        System.out.println(s);
        }

        private static void testPayURL() throws Exception {
            final String orderid = UUID.randomUUID().toString().replaceAll("-", "");
//        final String payUrl = WxpayUtils.getPayUrl("20230127111905xy");
            final String payUrl = WxPayUtils.getPayUrl(orderid);
            System.out.println(payUrl);
        }

    /**
     * 生成二维码路径
     * @param orderId
     * @return
     * @throws Exception
     */
        public static String getPayUrl(String orderId) throws Exception {
            //设置当前订单信息
            HashMap<String, String> data = new HashMap<>();
            data.put("body", "testproduct");  //商品描述
            data.put("out_trade_no", orderId);               //使用当前用户订单的编号作为当前支付交易的交易号
            data.put("fee_type", "CNY");                     //支付币种
            //data.put("total_fee",order.getActualAmount()*100+"");          //支付金额
            data.put("total_fee", "1");
            data.put("trade_type", "NATIVE");                //交易类型
            data.put("notify_url", "http://47.118.45.73:8080/pay/callback");           //设置支付完成时的回调方法接口

            //发送请求，获取响应
            //微信支付：申请支付连接
            WXPay wxPay = new WXPay(new MyPayConfig());
            Map<String, String> resp = wxPay.unifiedOrder(data);
            System.out.println(resp);
            return resp.get("code_url");
        }

    /**
     * 订单的状态
     * @param orderId
     * @return
     * @throws Exception
     */
        public static String queryOrder(String orderId) throws Exception {
            HashMap<String, String> params = new HashMap<>();
            params.put("out_trade_no", orderId);
            WXPay wxPay = new WXPay(new MyPayConfig());
            Map<String, String> resp = wxPay.orderQuery(params);
            return resp.get("trade_state");
        }
    }
